﻿using System;
using System.Collections.Generic;
using System.Linq;
using Nest;

namespace Infrastructure.ElasticSearch.ParentChilds
{
    public class EsSearchHasChildQuery<TP,TC> : ISearchSpecification<ISearchRequest<TP>> where TP : class  where TC : class
    {
        private readonly ISearchSpecification<ISearchRequest<TC>> _specifications;

        public EsSearchHasChildQuery(ISearchSpecification<ISearchRequest<TC>> specifications)
        {
            _specifications = specifications;
        }

        public bool IsNest { get; set; }

        public ISearchRequest<TP> IsSatisfiedBy()
        {
            return new SearchDescriptor<TP>()
                .Query(q => q
                    .HasChild<TC>(m => m
                        .Type(nameof(TC).ToCamelCase())
                        .Query(r=>
                            _specifications.IsSatisfiedBy().Query
                        )
                    )
                );
        }
    }
}